package com.atguigu.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.TravelItemDao;
import com.atguigu.entity.PageResult;

import com.atguigu.pojo.TravelItem;
import com.atguigu.service.TravelItemService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = TravelItemService.class)
@Transactional
public class TravelItemServiceImpl implements TravelItemService {
    @Autowired
    TravelItemDao travelItemDao;
    @Override
    public void add(TravelItem travelItem) {

        travelItemDao.add(travelItem);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页功能(分页插件)
        PageHelper.startPage(currentPage, pageSize);
        //分页条件查询 注意返回的是page
        Page page = travelItemDao.findPage(queryString);
        //封装返回结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        //在删除自由行之前，先判断自由行的id,在中间表中是否存在数据
        long count = travelItemDao.findCountByTravelItemItemId(id);
        //中间表如果有数据，不要往后面执行，直接抛出异常
        //如果非要删除也可以:delete from t_travelgroup_travelitem where travelitem_id = 1
        if (count>0) {
            throw new RuntimeException("数据存在关联，不能删除，请重新输入");
        }
        //使用自由行的id进行删除
        travelItemDao.delete(id);
    }

    @Override
    public TravelItem get(Integer id) {
       return travelItemDao.get(id);

    }

    @Override
    public void edit(TravelItem travelItem) {
        travelItemDao.edit(travelItem);
    }

    @Override
    public List<TravelItem> findAll() {
        return travelItemDao.findAll() ;
    }


}

